﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Models;
using FCTC_Web.DAL;
using DAL;
using FCTC_Web.Content;
using FCTC_Web.Utilities;
using System.Reflection;

namespace FCTC_Web.Controllers.Maintains
{
    [HandleError]
    public class EstimatorController : Controller
    {
        public ViewResult Index(int? pageId)
        {
            return View("Index", Pager6.Ins.GetPage<Estimator>(pageId));
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Estimator estimator, Personal p)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    CH.Create<Personal>(p);
                    estimator.PersonalID = p.ID;
                    CH.Create<Estimator>(estimator);
                    var pageId = Pager6.Ins.GetPageId<Estimator>(estimator.ID);
                    return RedirectToAction("Index", new { pageId = pageId });
                }
            }
            catch (DataException ex)
            {
                //Log the error (add a variable name after DataException)
                ModelState.AddModelError("", StringResources.GeneralDataOperationError);
            }
            InitialDDL(estimator);
            return View(estimator);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Estimator estimator, Personal p)
        {
            if (ModelState.IsValid)
            {
                p.ID = (int)estimator.PersonalID;
                CH.Edit<Personal>(p);
                CH.Edit<Estimator>(estimator);
                var pageId = Pager6.Ins.GetPageId<Estimator>(estimator.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            InitialDDL(estimator);
            return View(estimator);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var pageId = Pager6.Ins.GetPageId<Estimator>(id);
            var e = CH.GetDataById<Estimator>(id);
            if (ModelState.IsValid)
            {
              
                var personalId = e.PersonalID;
                CH.Delete<Estimator>(id);
                CH.Delete<Personal>(personalId);

                return RedirectToAction("Index", new { pageId = pageId });
            }
            return View(e);
        }

        private void InitialDDL(Estimator estimator = null)
        {
            if (estimator == null)
            {
                ViewBag.ShopID = DrapDownListHelper.InitialSelectionDropDownList<Shop>("ID", "ID", "FullName");
                return;
            }
            ViewBag.ShopID = DrapDownListHelper.InitialSelectionDropDownList<Shop>("ID", "ID", "FullName", estimator.ShopID);
        }

        public ViewResult Details(int? id)
        {
            Estimator estimator = CH.GetDataById<Estimator>(id);
            return View(estimator);
        }

        public ActionResult Create()
        {
            InitialDDL();
            return View();
        }

        public ActionResult Edit(int? id)
        {
            if (id == null) return View();

            var estimator = CH.GetDataById<Estimator>(id);
            InitialDDL(estimator);

            return View(estimator);
        }

        public ActionResult Delete(int? id)
        {
            Estimator estimator = CH.GetDataById<Estimator>(id);
            return View(estimator);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}